﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel; // INotifyPropertyChanged
using System.Collections.Generic; // IEnumerable
using System.Collections.ObjectModel; // ObservableCollection

using System.Windows.Media.Imaging; // BitmapImage

using TweetSharp;

namespace MyMP3
{
    // a wrapper for TwitterStatus
    public class TweetInfo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ImageSource _profileImageSource;
        private string _timeSent;
        private string _text;
        private string _sender;

        public ImageSource ProfileImageSource
        {
            get
            {
                return _profileImageSource;
            }
            set
            {
                if (_profileImageSource != value)
                {
                    _profileImageSource = value;
                    OnPropertyChanged("ProfileImageSource");
                }
            }
        }

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    OnPropertyChanged("Text");
                }
            }
        }

        public string TimeSent
        {
            get
            {
                return _timeSent;
            }
            set
            {
                if (_timeSent != value)
                {
                    _timeSent = value;
                    OnPropertyChanged("TimeSent");
                }
            }
        }

        public string Sender
        {
            get
            {
                return _sender;
            }
            set
            {
                if (_sender != value)
                {
                    _sender = value;
                    OnPropertyChanged("Sender");
                }
            }
        }

        protected virtual void OnPropertyChanged(string propChanged)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
            }
        }
    }

    public class TweetPresenter : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public TweetPresenter()
        {
        }

        public TweetPresenter(IEnumerable<TwitterStatus> statuses)
        {
            RefreshTweets(statuses);
        }

        ObservableCollection<TweetInfo> _tweetInfoCollection = new ObservableCollection<TweetInfo>();
        public ObservableCollection<TweetInfo> TweetInfoCollection
        {
            set
            {
                if (_tweetInfoCollection != value)
                {
                    _tweetInfoCollection = value;
                    OnPropertyChanged("TweetInfoCollection");
                }
            }
            get
            {
                return _tweetInfoCollection;
            }
        }

        protected virtual void OnPropertyChanged(string propChanged)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propChanged)); // public delegate void PropertyChangedEventHandler(Object sender, PropertyChangedEventArgs e)
            }
        }

        public void RefreshTweets(IEnumerable<TwitterStatus> statuses)
        {
            _tweetInfoCollection.Clear();

            // populate _tweetInfoCollection
            foreach (var tweet in statuses)
            {
                TweetInfo tweetInfo = new TweetInfo();

                tweetInfo.ProfileImageSource = new BitmapImage(new Uri(tweet.User.ProfileImageUrl));
                tweetInfo.Sender = tweet.User.ScreenName;
                tweetInfo.TimeSent = string.Format("sent {0}", tweet.CreatedDate.ToShortDateString());
                tweetInfo.Text = tweet.Text;

                _tweetInfoCollection.Add(tweetInfo);
            }
        }

        public void InsertLocalMessage(string message)
        {
            TweetInfo tweetInfo = new TweetInfo();

            tweetInfo.ProfileImageSource = null;
            tweetInfo.Sender = "";
            tweetInfo.TimeSent = "";
            tweetInfo.Text = message;

            _tweetInfoCollection.Add(tweetInfo);
        }
    }
}
